﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lucene.Net.Analysis;

namespace ERPStore.Indexers.Lucene.Services
{
	public class CompensatorFilter : TokenFilter
    {
        private Queue<Token> synonymTokenQueue = new Queue<Token>();

		public CompensatorFilter(TokenStream input, ICompensatorRepository compensatorRepository)
            : base(input)
        {
			this.CompensatorRepository = compensatorRepository;
        }

		protected ICompensatorRepository CompensatorRepository { get; private set; }

        public override Token Next()
        {
            Token t = input.Next();
			if (t == null)
			{
				return null;
			}

			var compensation = CompensatorRepository.GetCompensation(t.TermText());
			if (compensation == null)
            {
                return t;
            }

			t = new Token(compensation, t.StartOffset(), t.EndOffset(), "<COMPENSATOR>");

            return t;
        }
	}
}
